var renderTable;
var form;
var insTb;
var ctxPath = '/'
$(function () {
    layui.config({
        base: '/' + 'lib/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index',//主入口模块
        treeTable: 'treetable-lay/2.x/treeTable'
    }).use(['index', 'form', 'treeTable'], function () {
        form = layui.form;
        var treeTable = layui.treeTable;
        insTb = treeTable.render({
            elem: '#table',
            toolbar: '#table-title',
            reqData: function (data, callback) {
                $.ajax({
                    type: "GET",
                    url: "organList",
                    data: {
                        "cyqxh": $("#cyqxhSearch").val(),
                        "name": $("#nameSearch").val()
                    },
                    success: function (res) {
                        callback(res.obj);  // 参数是数组类型
                    }
                });
            },
            tree: {
                iconIndex: 1,  // 折叠图标显示在第几列
                idName: 'cyqxh', //设定id的字段名
                pidName: 'porgan', //设定pid的字段名
                isPidData: true  // 是否是pid形式数据
            },
            cols: [
                {type: 'numbers'},
                {field: 'cyqxh', title: '查验区序号', align: 'center'},
                {field: 'name', title: '查验区名称', width: 200},
                {field: 'seq', title: '排序', width: 90, align: 'center'},
                {field: 'address', title: '地址', align: 'center'},
                {field: 'organtype', title: '机构类型', align: 'center', templet: '#isOrganType'},
                {field: 'right', title: '操作', toolbar: '#table-toolbar', align: 'center'},
            ],
        });
        //监听行工具事件
        treeTable.on('tool(table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                openDel(obj)
            } else if (obj.event === 'edit') {
                openEdit(data)
            } else if (obj.event === 'add') {
                openAdd(data);
            }
        });
        //监听录像下载配置
        form.on('select(lxxzpz)', function (data) {
            //选择不下载，隐藏IIS虚拟路径标签
            if (data.value == "0") {
                $("#zdfwqdzDiv").hide();
            } else {
                $("#zdfwqdzDiv").show(); //选择下载，展示IIS虚拟路径标签
            }
        });
        //监听搜索提交
        form.on('submit(searchSubmit)', function () {
            insTb.refresh();
            return false;
        });
        form.on('submit(submitOrgan)', function (data) {
            organSubmit($("#oper").val())
        })
    });
});


function submitCancel() {
    layer.closeAll()

}
/**
 * 删除机构
 * @param obj
 */
function openDel(obj) {
    var data = obj.data;
    layer.confirm('此操作将会删除' + data.name + '以及其下所有机构，请慎重操作', function () {
        $.ajax({
            type: "DELETE",
            data: {
                "cyqxh": data.cyqxh
            },
            url: ctxPath + "system/organization/organInfo",
            complete: function () {
                insTb.refresh();
            }
        });
    });
}

function openAdd(data) {
    layer.open({
        type: 1,
        title: "机构管理",
        fixed: false,
        resize: false,
        shadeClose: true,
        area: ['60%', '70%'],
        content: $('#organEdit'),
        success: function () {
            //写入父机构查验区序号
            $("input[name='porgan']").val(data.cyqxh);
            //新建功能解除禁用编辑查验区序号
            $("input[name='cyqxh']").removeAttr("readonly");
            $("#oper").val("0")
            //刷新表单
            renderForm();
        }
        , end: function () {
            $("#zdfwqdzDiv").hide();//隐藏IIS服务器地址输入框
            cleanForm("organForm");
        }
    })
}

/**
 * 打开受理页面
 * @param data layui中保存的table中的对应的此条信息
 */
function openEdit(data) {
    layer.open({
        type: 1,
        title: "机构管理",
        fixed: false,
        resize: false,
        shadeClose: true,
        area: ['60%', '70%'],
        content: $('#organEdit'),
        success: function () {

            //填充表单
            form.val('organForm', data);
            //不允许编辑查验区序号
            $("input[name='cyqxh']").attr("readonly", "readonly");
            $("#oper").val("1")
            renderForm()

        }, end: function () {
            $("#zdfwqdzDiv").hide();//隐藏IIS服务器地址输入框
            cleanForm("organForm");
        }
    });
}

/**
 * 表单提交
 * @param oper 0：新建操作  1：更新操作
 */
function organSubmit(oper) {
    var type = oper == "0" ? "POST" : "PUT";
    $.ajax({
        type: type,
        data: serializeObject($("#organForm")),
        url: ctxPath + "system/organization/organInfo",
        complete: function () {
            insTb.refresh();
        }
    });
}